package com.cz.ssyx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.ssyx.model.product.AttrGroup;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.product.service.AttrGroupService;
import com.cz.ssyx.vo.product.AttrGroupQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-04-18
 */
@Api(tags = "属性组管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/product/attrGroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @ApiOperation("获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit,
                        AttrGroupQueryVo attrGroupQueryVo) {
        String name = attrGroupQueryVo.getName();

        Page<AttrGroup> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<AttrGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name), AttrGroup::getName, name);

        Page<AttrGroup> attrGroupPage = attrGroupService.page(pageParam, queryWrapper);

        return Result.ok(attrGroupPage);
    }

    @ApiOperation("根据id获取属性组")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        AttrGroup attrGroup = attrGroupService.getById(id);
        return Result.ok(attrGroup);
    }

    @ApiOperation("新增属性组")
    @PostMapping("/save")
    public Result save(@RequestBody AttrGroup attrGroup) {
        attrGroupService.save(attrGroup);
        return Result.ok(null);
    }

    @ApiOperation("修改属性组")
    @PutMapping("/update")
    public Result updateById(@RequestBody AttrGroup attrGroup) {
        attrGroupService.updateById(attrGroup);
        return Result.ok(null);
    }

    @ApiOperation("删除属性组")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        attrGroupService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("根据id批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        attrGroupService.removeByIds(idList);
        return Result.ok(null);
    }

    @ApiOperation("获取全部属性分组")
    @GetMapping("/findAllList")
    public Result findAllList() {
        QueryWrapper<AttrGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<AttrGroup> list = attrGroupService.list(queryWrapper);
        return Result.ok(list);
    }
}

